const express = require('express')
const loginWay = express.Router()
const db = require('../mysql/db')
let dataRes = false
function query(data) {
    let account = data.account
    let code = data.code
    return new Promise((resolve, reject) => {
        db.query(`SELECT * FROM user WHERE account = ?`, [account], (err, results) => {
            if (results.length == 0 || err  ) {
                reject(false);
            } else {
                if (results[0].code == code) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            }
        });
    });
}

loginWay.post('/login', async (req, res) => {
    try {
        const resData = await query(req.body);
        res.send({
            code: 200,
            message: '请求成功！', // 状态的描述
            result: resData, // 需要响应给客户端的数据
        });
    } catch (err) {
        res.send({
            code: 500,
            message: '查询失败！账号不存在！',
            error: err.message,
        });
    }
})
module.exports = loginWay
